﻿using Application.DTO.Form;
using Application.DTO;
using Application.IService.Form;
using AutoMapper;
using Domain.Entity.Form;
using Infrastructure.IRepository;
using Infrastructure.IRepository.Form;
using Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Net.Sockets;
using Newtonsoft.Json;
using System.Xml;

namespace Application.Service.Form
{
    public class FormService : BaseService<FormDesignInfo>, IFormService
    {
        private readonly IFormRepository formRepository;
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper mapper;
        public FormService(IFormRepository formRepository, IUnitOfWork unitOfWork, IMapper mapper) : base(unitOfWork, formRepository)
        {
            this.formRepository = formRepository;
            this.unitOfWork = unitOfWork;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加表单数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDTO<string>> InsertForm(FormDesignerDTO dto)
        {
            var data = mapper.Map<FormDesignInfo>(dto);
            //将表单数据转为json
            //string json = JsonConvert.SerializeObject(dto.JsonFormData);
            data.CreateTime = DateTime.Now;
            //将json给data
            //data.JsonFormData = json;

            //添加表单信息
            await formRepository.Insert(data);
            //保存信息
            int h = await unitOfWork.SaveChanges();
            //判断是否成功
            if (h > 0)
            {
                return new ResultDTO<string>("", Domain.Enum.ResultStatusCode.SUCCESS, "添加成功");
            }
            else
            {
                return new ResultDTO<string>("", Domain.Enum.ResultStatusCode.HANDLEFAIL, "添加失败");
            }
        }
        /// <summary>
        /// 修改表单数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDTO<string>> UpdateForm(FormDesignerDTO dto)
        {
            var data = mapper.Map<FormDesignInfo>(dto);
            //string json = JsonConvert.SerializeObject(dto.JsonFormData);
            data.UpdateTime = DateTime.Now;
            //data.JsonFormData = json;

            //修改表单信息
            await formRepository.Update(data);
            //保存信息
            int h = await unitOfWork.SaveChanges();
            //判断是否成功
            if (h > 0)
            {
                return new ResultDTO<string>("", Domain.Enum.ResultStatusCode.SUCCESS, "修改成功");
            }
            else
            {
                return new ResultDTO<string>("", Domain.Enum.ResultStatusCode.HANDLEFAIL, "修改失败");
            }
        }
        /// <summary>
        /// 删除表单数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDTO<string>> DeleteForm(string ids)
        {
            //分割ID值
            string[] temp = ids.Split(',');
            int[] arrayIds = Array.ConvertAll<string, int>(temp, int.Parse);

            //遍历ID数组
            foreach (var id in arrayIds)
            {
                //查询表单是否存在
                var data = await (await formRepository.GetAll()).Where(m => m.Id == id).FirstOrDefaultAsync();
                if (data != null)
                {
                    data.IsDeleted = true;
                    //修改数据
                    formRepository.Update(data);
                }
            }
            //保存
            int h = await unitOfWork.SaveChanges();
            //判断是否成功
            if (h > 0)
            {
                return new ResultDTO<string>("", Domain.Enum.ResultStatusCode.SUCCESS, "删除成功");
            }
            else
            {
                return new ResultDTO<string>("", Domain.Enum.ResultStatusCode.HANDLEFAIL, "删除失败");
            }


        }
        /// <summary>
        /// 查询表单数据
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDTO<List<FormDesignerDTO>>> ListFormDesigner()
        {
            ///查询数据
            var list = (await formRepository.GetAll())
                .Where(m => (bool)!m.IsDeleted)
                .Select(m => new FormDesignerDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                    Describe = m.Describe,
                    CreateTime = m.CreateTime,
                    UpdateTime = m.UpdateTime,
                    JsonFormData =m.JsonFormData
                }
                ).ToList();

            //var data = mapper.Map<List<FormDesignerDTO>>(list);

            return new ResultDTO<List<FormDesignerDTO>>(list);
        }
    }
}
